using Microsoft.Win32.SafeHandles;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Windows.Win32;

namespace EasyWindowsTerminalControl.Internals {
	/// <summary>
	/// A pipe used to talk to the pseudoconsole, as described in:
	/// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session
	/// </summary>
	/// <remarks>
	/// We'll have two instances of this class, one for input and one for output.
	/// </remarks>
	public class PseudoConsolePipe : IDisposable {
		public readonly SafeFileHandle ReadSide;
		public readonly SafeFileHandle WriteSide;

		public PseudoConsolePipe() {
			if (!PInvoke.CreatePipe(out ReadSide, out WriteSide, null, 0)) {
				throw new Win32Exception(Marshal.GetLastWin32Error(), "failed to create pipe");
			}
		}

		#region IDisposable

		void Dispose(bool disposing) {
			if (disposing) {
				ReadSide?.Dispose();
				WriteSide?.Dispose();
			}
		}

		public void Dispose() {
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		#endregion
	}
}
